/**
 *
 * @Author: Marek Zakowicz <mazak (at) debian (dot) linux.org.pl>
 *
 * Copyright (c) 2005, 2006, 2007 Marek Zakowicz. All rights reserved.
 *
 * Licence: This code may be used, (re)distribute and/or modify under the terms one of the following two kinds of licence:
 *          1) as free software under GPL 3 (General Public Licence ver. 3 as published by the Free Software Foundation)
 *          2) as software under the terms individual licence from Marek Zakowicz
 *
 *          This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *          without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */

package jsp2php;

import java.util.*;

class AddOns {
   private       static Set<String>         used   = new TreeSet<String>(); // Add ons should be generated in the same order - it minimizes regression tests changes.
   private       static Map<String, String> addons = new HashMap<String, String>(); // PHP 4 name space is flat.  So it's a good idea to call addons as their PHP names.
   private final static String              begin  = "<?\n// jsp2php add ons\n";
   private final static String              end    = "?>\n";

   private AddOns() {}

   // Add new item
   public static void add(String id, String code) {
      // TODO: throw exceptiond if id exists
      addons.put(id, code);
   }

   // Use item
   public static void use(String id) {
      used.add(id);
   }

   // Clear information about add ons use
   public static void clear() {
      used.clear();
   }

   // Generate addons
   public static String generate() {
      StringBuilder sb = new StringBuilder(1024);

      sb.append(begin);
      for(String id : used) {
         sb.append( addons.get(id) ).append("\n");
      }
      if( sb.length()==begin.length() ) {
         return "";
      }
      sb.append(end);
      return sb.toString();
   }
}
